﻿using System;
using System.Collections.Generic;
using Bingosoft.Data;
using Bingosoft.Modules.SecurityConsole.Common.Helper;

namespace Bingosoft.WebApp.Modules.SecurityConsole.Common.FunctionControl.ListSelect
{
    public partial class ListSelect : System.Web.UI.ListPage
    {
        public string groupListString = "";
        public string selectedListString = "";
        public string selectListString = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            Dao dao = Dao.Get();

            //处理传递参数问题
            string paramsString = Request.Params["params"];
            IDictionary<string, object> parameters = DictionaryHelper.ConvertJsonStringToDictionary(paramsString);
            
            //初始化分类下拉框的数据
            string groupDataSourceType = Request.Params["groupDataSourceType"];
            string groupDataSource     = Request.Params["groupDataSource"];

            //格式：[{id:xx,text:xxx},{id:xx,text:xxx}]
            if ("sql".Equals(groupDataSourceType))
            {
                groupListString += "[";
                IList<IDictionary<string, object>> catalogList = dao.QueryDictionaries(groupDataSource, parameters);
                foreach (IDictionary<string, object> item in catalogList)
                {
                    string id = DictionaryHelper.GetParamString(item, "ID");
                    string text = DictionaryHelper.GetParamString(item, "TEXT");
                    if (!"[".Equals(groupListString))
                    {
                        groupListString += ",";
                    }
                    groupListString += "{'id':'" + id + "','text':'" + text + "'}";

                }
                groupListString += "]";
            }
            else if("value".Equals(groupDataSourceType))
            {
                groupListString = groupDataSourceType;
            }

            //初始化已选列表的数据
            string selectedDataSourceType = Request.Params["selectedDataSourceType"];
            string selectedDataSource     = Request.Params["selectedDataSource"];
            //格式：[{id:xx,text:xxx},{id:xx,text:xxx}]
            if ("sql".Equals(selectedDataSourceType))
            {
                selectedListString += "[";
                IList<IDictionary<string, object>> selectedList = dao.QueryDictionaries(selectedDataSource, parameters);
                foreach (IDictionary<string, object> item in selectedList)
                {
                    string id = DictionaryHelper.GetParamString(item, "ID");
                    string text = DictionaryHelper.GetParamString(item, "TEXT");
                    if (!"[".Equals(selectedListString))
                    {
                        selectedListString += ",";
                    }
                    selectedListString += "{'id':'" + id + "','text':'" + text + "'}";
                }
                selectedListString += "]";
            }
            else if ("value".Equals(selectedDataSourceType))
            {
                selectedListString = selectedDataSource;
            }

            //初始化待选列表数据
            string selectDataSourceType = Request.Params["selectDataSourceType"];
            string selectDataSource = Request.Params["selectDataSource"];
            //格式：[{id:xx,text:xxx},{id:xx,text:xxx}]
            if ("sql".Equals(selectDataSourceType))
            {
                selectListString += "[";
                IList<IDictionary<string, object>> selectList = dao.QueryDictionaries(selectDataSource, parameters);
                foreach (IDictionary<string, object> item in selectList)
                {
                    string id = DictionaryHelper.GetParamString(item, "ID");
                    string text = DictionaryHelper.GetParamString(item, "TEXT");
                    selectListString += "{'id':'" + id + "','text':" + text + "'}";

                }
                selectListString += "]";
            }
            else if ("value".Equals(selectDataSourceType))
            {
                selectListString = selectDataSource;
            }

        }
    }
}